<template>
  <div
    class="relative"
    @mouseenter="onMouseenter"
    @mouseleave="onMouseleave"
  >
    <div ref="referenceTarget">
      <!-- 具名插槽：触发弹窗的视图 -->
      <slot name="reference" />
    </div>
    <!-- 气泡展示 -->
    <transition name="slide">
      <div
        ref="contentTarget"
        v-show="isViable"
        class="absolute p-1 z-20 bg-white dark:bg-zinc-900 border rounded-md dark:border-zinc-700"
        :style="contentStyle"
      >
        <!-- 匿名插槽：弹出层视图中展示的内容 -->
        <slot />
      </div>
    </transition>
  </div>
</template>

<script>
// 延时关闭时间
const DELAY_TIME = 100
// 左上
const PROP_TOP_LEFT = 'top-left'
// 右上
const PROP_TOP_RIGHT = 'top-right'
// 左下
const PROP_BOTTOM_LEFT = 'bottom-left'
// 右下
const PROP_BOTTOM_RIGHT = 'bottom-right'

const placementEnum = [
  PROP_TOP_LEFT,
  PROP_TOP_RIGHT,
  PROP_BOTTOM_LEFT,
  PROP_BOTTOM_RIGHT,
]
</script>

<script setup>
import { nextTick, ref, watch } from 'vue'

/**
 * 1、指定所有可选位置的常量，并生成 enum
 * 2、通过 prop 控制指定位置
 * 3、获取元素的 DOM。创建读取元素尺寸的方法
 * 4、生成气泡的样式对象，用来 控制每个位置对应的样式
 * 5、根据 prop，计算样式对象
 */

const props = defineProps({
  placement: {
    type: String,
    default: PROP_TOP_LEFT,
    validator(val) {
      const result = placementEnum.includes(val)
      if (!result) {
        throw new Error(`你的 placement 必须是 ${placementEnum.join('、')} 中的一个`)
      }
      return result
    }
  }
})


const isViable = ref(false)

/**
 * 鼠标移入
*/
let timeout
const onMouseenter = () => {
  isViable.value = true
  if (timeout) {
    clearTimeout(timeout)
  }
}
/**
 * 鼠标移出
*/
const onMouseleave = () => {
  timeout = setTimeout(() => {
    isViable.value = false
    timeout = null
  }, DELAY_TIME)
}

/**
 * 计算元素的尺寸
 */
 const referenceTarget = ref(null)
const contentTarget = ref(null)
const getElementSize = (target) => {
  if (!target) return {}
  return {
    width: target.offsetWidth,
    height: target.offsetHeight
  }
}

const contentStyle = ref({
  top: 0,
  left: 0
})

// 计算：期望气泡展示的时候再进行计算
watch(isViable, (val) => {
  if (!val) return
  nextTick(() => {
    switch (props.placement) {
      // 左上
      case PROP_TOP_LEFT:
        contentStyle.value.top = 0
        contentStyle.value.left = -getElementSize(contentTarget.value).width + 'px'
        break
      // 右上
      case PROP_TOP_RIGHT:
        contentStyle.value.top = 0
        contentStyle.value.left = getElementSize(referenceTarget.value).width + 'px'
        break
      // 左下
      case PROP_BOTTOM_LEFT:
        contentStyle.value.top = getElementSize(referenceTarget.value).height + 'px'
        contentStyle.value.left = -getElementSize(contentTarget.value).width + 'px'
        break
      // 右下
      case PROP_BOTTOM_RIGHT:
        contentStyle.value.top = getElementSize(referenceTarget.value).height + 'px'
        contentStyle.value.left = getElementSize(referenceTarget.value).width + 'px'
        break
    }
  })
})
</script>

<style lang="scss" scoped>
.slide-enter-active,
.slide-leave-active {
  transition: opacity .3s, transform .3s;
}

.slide-enter-from,
.slide-leave-to {
  opacity: 0;
  transform: translateY(20px);
}
</style>
